﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OA.Core.Helper
{
    public static class Base64Helper
    {
        public static string ToBase64(this string original, bool inverted = false)
        {
            if (string.IsNullOrWhiteSpace(original)) return string.Empty;
            byte[] b = System.Text.Encoding.Default.GetBytes(original);
            return Convert.ToBase64String(b);
        }

        public static T FromBase64<T>(this string original, bool inverted = false)
        {
            if (string.IsNullOrWhiteSpace(original)) return default(T);
            byte[] bytes = Convert.FromBase64String(original);
            var decode = Encoding.Default.GetString(bytes);
            return JsonHelper.DeserializeObj<T>(decode);
        }

        public static string FromBase64(this string original, bool inverted = false)
        {
            if (string.IsNullOrWhiteSpace(original)) return string.Empty;
            byte[] bytes = Convert.FromBase64String(original);
            return Encoding.Default.GetString(bytes);
        }
    }
}
